package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.tanhua.dubbo.mappers.SoulQuestionMapper;
import com.tanhua.model.domain.SoulQuestion;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class SoulQuestionApiImpl implements SoulQuestionApi{
    @Autowired
    private SoulQuestionMapper soulQuestionMapper;

    @Override
    public Map<Long, SoulQuestion> question(List<Long> questionId) {
        LambdaQueryWrapper<SoulQuestion> lqw = new LambdaQueryWrapper<>();
        lqw.in(SoulQuestion::getQuestionnaireId, questionId);
        List<SoulQuestion> soulQuestions = soulQuestionMapper.selectList(lqw);
        Map<Long, SoulQuestion> soulQuestionMap = new HashMap<>();
        for (SoulQuestion soulQuestion : soulQuestions) {
            soulQuestionMap.put(soulQuestion.getId(), soulQuestion);
        }
        return soulQuestionMap;
    }

    @Override
    public Long getQuestionnaire(List<Long> questionIds) {
        String s = questionIds.toString();
        int end = s.lastIndexOf("]");
        int index = s.indexOf("[");
        String substring = s.substring(index+1, end);
        ArrayList<Long> questionnaire = soulQuestionMapper.getQuestionnaire(substring);
        if (questionnaire == null || questionnaire.size() == 0) {
            return null;
        }
        return questionnaire.get(0);
    }
}
